package com.bennu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bennu.common.BaseContext;
import com.bennu.common.R;
import com.bennu.domain.ShoppingCart;
import com.bennu.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody  ShoppingCart shoppingCart){
        log.info( shoppingCart.toString() );
        //设置用户id，指定用户的购物车
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId( currentId );
        //查询当前菜品或套餐是否在购物车中
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq( ShoppingCart::getUserId,currentId );
        if ( dishId!=null ){
            //添加到购物车的是菜品
            queryWrapper.eq( ShoppingCart::getDishId,dishId );
        }else {
            //添加到购物车的是套餐
            queryWrapper.eq( ShoppingCart::getSetmealId,shoppingCart.getSetmealId() );
        }
        ShoppingCart shoppingCartServiceOne = shoppingCartService.getOne( queryWrapper );
        //如果存在在原来基础上加一
        if ( shoppingCartServiceOne!=null ){
            //如果已经存在在数量上加一
            Integer number = shoppingCartServiceOne.getNumber();
            shoppingCartServiceOne.setNumber( number+1 );
            shoppingCartService.updateById( shoppingCartServiceOne );
        }else {
            //如果不存在添加一条
            shoppingCart.setNumber( 1 );
            shoppingCart.setCreateTime( LocalDateTime.now() );
            shoppingCartService.save( shoppingCart );
            shoppingCartServiceOne=shoppingCart;
        }
        return R.success( shoppingCartServiceOne);
    }
    //查询购物车
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        log.info( "查看购物车" );
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq( ShoppingCart::getUserId,BaseContext.getCurrentId() );
        queryWrapper.orderByAsc( ShoppingCart::getCreateTime );
        List<ShoppingCart> list = shoppingCartService.list( queryWrapper );
        return R.success( list );
    }
    //清空购物车
    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq( ShoppingCart::getUserId,BaseContext.getCurrentId() );
        shoppingCartService.remove( queryWrapper );
        return R.success( "清空成功" );
    }
    //减少菜品数量
    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        log.info( shoppingCart.toString() );
        //设置用户id，指定用户的购物车
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId( currentId );
        //查询当前菜品或套餐是否在购物车中
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq( ShoppingCart::getUserId,currentId );
        if ( dishId!=null ){
            //减少购物车的是菜品
            queryWrapper.eq( ShoppingCart::getDishId,dishId );
        }else {
            //减少购物车的是套餐
            queryWrapper.eq( ShoppingCart::getSetmealId,shoppingCart.getSetmealId() );
        }
        ShoppingCart shoppingCartServiceOne = shoppingCartService.getOne( queryWrapper );
        if ( shoppingCartServiceOne!=null ){
            Integer number = shoppingCartServiceOne.getNumber();
            shoppingCartServiceOne.setNumber( number-1 );
            shoppingCartService.updateById( shoppingCartServiceOne);
            boolean b = shoppingCart.getNumber() == null;
            if (b){
                shoppingCartServiceOne.setNumber( number-1 );
                shoppingCartService.remove( queryWrapper );
            }
        }
        return R.success( "菜品数量减少成功" );
    }
}
